package _interview150;

import _object.ListNode;

/**
 * 2. 两数相加
 */
public class No2 {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        boolean flag = false;
        ListNode cur = new ListNode(-1);
        ListNode dummy = cur;
        while (l1 != null || l2 != null) {
            int curSum = (l1 != null ? l1.val : 0) + (l2 != null ? l2.val : 0) + (flag ? 1 : 0);
            flag = curSum >= 10;
            cur.next = new ListNode(curSum % 10);
            cur = cur.next;
            if (l1 != null) l1 = l1.next;
            if (l2 != null) l2 = l2.next;
        }
        if (flag) cur.next = new ListNode(1);

        return dummy.next;
    }
}
